﻿using System;
using ATMConst;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace Package
{
	[Serializable]
	public class Package
	{
		public Const.Code code { get; protected set; }
		public object data { get; protected set; }
		public Package(Const.Code code, Object data)
		{
			this.code = code;
			this.data = data;
        }
		public static byte[] serializePackage(Package package)
		{
			var bFormatter = new BinaryFormatter();
			var stream = new MemoryStream();
            bFormatter.Serialize(stream, package);
			var buffer = new byte[stream.Length];
            stream.Seek(0, SeekOrigin.Begin);
			stream.Read(buffer, 0, (int)stream.Length);
			return buffer;
		}
		static public Package deserializePackage(byte[] bytes)
		{
			var bFormatter = new BinaryFormatter();
			var stream = new MemoryStream(bytes);
			Package package =  bFormatter.Deserialize(stream, null) as Package;
			/*if (package != null && package.code == Const.Code.CELL)
				return CellPackage.deserializeCellPackageArray(bytes);
			Package[] packages = new Package[1];
			packages[0] = package;
			return packages;*/
			return package;
		}

	}
}
